#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:jidi
@file:contract_page.py
@time:2022/08/29
"""
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from web.wework.framework.base_page import BasePage
from web.wework.page.member import Member
from web.wework.page.member_page import MemberPage


class ContactPage(BasePage):
    def __init__(self, driver: WebDriver):
        super().__init__(driver)

        WebDriverWait(self.driver, 10) \
            .until(expected_conditions.visibility_of_element_located(
            (By.LINK_TEXT, '立即邀请')
        ))

    def add_member(self, member: Member):
        self.click(By.LINK_TEXT, '添加成员')

        WebDriverWait(self.driver, 10).until(
            expected_conditions.visibility_of_element_located((By.NAME, 'username')))
        self.find(By.NAME, 'username').send_keys(member.name)
        self.find(By.NAME, 'acctid').send_keys(member.id)

        mail_elements = self.find(By.NAME, 'biz_mail')
        mail_elements.clear()
        mail_elements.send_keys(member.mail)

        self.find(By.NAME, 'mobile').send_keys(member.phone)
        self.find(By.LINK_TEXT, '保存').click()

        WebDriverWait(self.driver, 10) \
            .until(expected_conditions.visibility_of_element_located(
            (By.LINK_TEXT, '立即邀请')
        ))

        return self

    def import_contract(self, path):
        return self

    def export_contract(self):
        ...

    def del_member(self):
        return self

    def search_member(self, keyword):
        self.find(By.ID, 'memberSearchInput').send_keys(keyword)

        return MemberPage(self.driver)
